﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.Admin
{
    public partial class Admin_InstructorModify : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string enteredID;
        static string enteredName;

        string strProvider = Resource1.databaseConnection;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[1].Selected = true;
            Master.FindControl("ContentPlaceholder1").FindControl("Button2").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button3").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button4").Visible = false;

            enteredID = Request.QueryString["id"];

            if (!IsPostBack)
            {
                // Connect to the Database
                conn = new MySqlConnection(strProvider);
                conn.Open();

                // Get the type


                string comma = ", ";

                string getSelectedInfo = "SELECT *"
                                     + "FROM Staff S, Instructor I "
                                     + "WHERE S.fsid = " + enteredID + " AND I.ins_fsid = S.fsid";

                sqlCommand = new MySqlCommand(getSelectedInfo, conn);
                readVals = sqlCommand.ExecuteReader();

                readVals.Read();
                ID.Text = "" + readVals.GetInt32("fsid");
                Name.Text = "" + readVals.GetString("fs_name");
                enteredName = Name.Text;
                BDate.Text = "" + readVals.GetDateTime("fs_birthDate").ToString("dd-MM-yyyy");
                EDate.Text = "" + readVals.GetDateTime("fs_entryDate").ToString("dd-MM-yyyy");
                Email.Text = "" + readVals.GetString("fs_email");

                DropDownList1.SelectedValue = "No";
                if (readVals.GetString("doctorate") == "True")
                {
                    DropDownList1.SelectedValue = "Yes";
                }
                DropDownList1.SelectedValue = "" + readVals.GetString("doctorate");
                Password.Text = "" + readVals.GetString("fs_pass");
                ImgUrl.Text = readVals.GetString("fs_url");

                readVals.Close();

            }
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            /*Get info from textboxes*/

            string id = ID.Text;
            string name = Name.Text;
            string rank = DropDownList1.SelectedValue.ToString();
            int doctorate = 0;

            if (rank == "Yes")
            {
                doctorate = 1;
            }

            DateTime bdate = Convert.ToDateTime(BDate.Text);
            string bdateInfo = bdate.Year + "-" + bdate.Month + "-" + bdate.Day;

            DateTime edate = Convert.ToDateTime(EDate.Text);
            string edateInfo = edate.Year + "-" + edate.Month + "-" + edate.Day;

            string email = Email.Text;
            string pass = Password.Text;
            string url = ImgUrl.Text;


            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string comma = ", ";

            string modifyStaff = "UPDATE Staff "
                                 + " SET  fsid=" + id + comma + " fs_name=" + "'" + name + "'" + comma + "fs_birthDate=" + "'" + bdateInfo + "'" + comma + "fs_entryDate=" + "'" + edateInfo + "'" + comma + "fs_email=" + "'" + email + "'" + comma + "fs_pass=" + "'" + pass + "'" + comma + "fs_url=" + "'" + url + "'"
                                 + " WHERE fsid=" + enteredID;

            string modifyfMember = "UPDATE Instructor "
                                 + "SET  doctorate=" + "'" + doctorate + "'" + ", ins_fsid=" + id 
                                 + " WHERE ins_fsid=" + enteredID;


            sqlCommand = new MySqlCommand(modifyStaff, conn);

            Boolean checker = false;
            try
            {
                sqlCommand.ExecuteNonQuery();

                sqlCommand = new MySqlCommand(modifyfMember, conn);

                sqlCommand.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {

                string msg = "Insert Error:";
                msg += ex.Message;
                ErrorLiteral.Text = msg;
                ErrorLiteral.Visible = true;
                checker = true;

            }

            if (!checker)
            {
                Response.Redirect("Admin_Instructor.aspx");
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin_Instructor.aspx");
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            string url = "../editHousing.aspx?name=" + enteredName + "&type=staff&id=" + enteredID;

            ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");

        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            string url = "../editOffice.aspx?name=" + enteredName + "&type=staff&id=" + enteredID;

            ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            string url = "../editCourses.aspx?name=" + enteredName + "&type=staff&id=" + enteredID;

            ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
        }
    }
}